package com.mach.platform.api.controller;

import com.mach.platform.api.service.AttentServiceImpl;
import com.mach.platform.api.service.MonitorServiceImpl;
import com.mach.platform.api.util.ImageUtil;
import com.mach.platform.domain.PageVo;
import com.mach.platform.domain.Response;
import com.mach.platform.domain.iot.IotDev;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api(tags = {"小程序监控页面接口"})
@RestController
@RequestMapping("/api/monitor")
public class MonitorCtrl {

    @Autowired
    private MonitorServiceImpl monitorService;

    @Autowired
    private AttentServiceImpl attentService;

    /**
     * 监控页面顶部下拉框数据(能源站房)
     * @return
     */
    @ApiOperation(value = "监控-顶部下拉框数据-获取所有的站房数据：包括空压，制冷，供水，供电，燃气，蒸汽")
    @PostMapping("/stationList")
    public Response stationList(HttpServletRequest request) {
        String openId = request.getHeader("openId");
        Map[] result = monitorService.stationList(openId);
        return Response.ok(result);
    }


    /**
     * 获取能源站房下所有的菜单列表
     * @param id 站房id
     * @return
     */
    @ApiOperation(value = "获取指定能源站房下所有的机组菜单列表")
    @PostMapping("/getStationMenu")
    public Response getStationMenu(Long id) throws SQLException {
        Map[] result = monitorService.getStationMenu(id);
        return Response.ok(result);
    }


    /**
     * 查询机房下控制设备的实时数据
     * @param id 机组菜单id
     * @return
     */
    @ApiOperation(value = "机房下控制设备的实时数据")
    @PostMapping("/controlDeviceData")
    public Response controlDeviceData(HttpServletRequest request, Long id) {
        String openId = request.getHeader("openId");
        Response checkResult = monitorService.checkItemParams(id);
        if (checkResult.getStatus() != 200) {
            return checkResult;
        }
        IotDev iotDev = (IotDev) checkResult.getData();
        Map result = monitorService.controlDeviceData(openId, id, iotDev);
        return Response.ok(result);
    }


    /**
     * 查询机房下机组设备的分页实时数据
     * @param pageable 分页参数，包括 page：当前页码，从0开始  size：每页显示记录数
     * @param id 机组菜单id
     * @return
     */
    @ApiOperation(value = "查询机房下机组设备的分页实时数据")
    @PostMapping("/unitDeviceData")
    public Response unitDeviceData(HttpServletRequest request, Pageable pageable, Long id) {
        String openId = request.getHeader("openId");
        PageVo pageResult = monitorService.unitDeviceData(pageable, id, openId);
        return Response.ok(pageResult);
    }


    /**
     * 关注机组或者设备
     * @param request
     * @param id 机组菜单id或者设备id
     * @param type 关注类型 1.设备组 2.设备
     * @param stationName 所属能源站房
     * @param unitName 机组菜单名称
     * @param devName 设备名称，如果关注设备，需要传这个参数
     * @return
     */
    @ApiOperation(value = "关注机组或者设备")
    @PostMapping("/attentUnitDevice")
    public Response attentUnitDevice(HttpServletRequest request, Long id, int type, String stationName, String unitName, String devName) {
        String openId = request.getHeader("openId");
        attentService.attentDevice(openId, id, type, stationName, unitName, devName);
        return Response.ok("关注成功");
    }


    /**
     * 取消关注机组或者设备
     * @param request 请求信息
     * @param type 取消关注类型 1.机组 2. 设备
     * @param id 机组菜单id或者设备id
     */
    @ApiOperation(value = "取消关注机组或者设备")
    @PostMapping("/cancelAttentUnitDevice")
    public Response cancelAttentUnitDevice(HttpServletRequest request, int type, Long id) {
        String openId = request.getHeader("openId");
        int count = attentService.cancelAttentDevice(openId, type, id);
        if (count != 1) {
            Response.error("取消关注失败");
        }
        return Response.ok("取消关注成功");
    }


    /**
     * 修改控制设备的上限值
     * @param id 控制设备的id
     * @param value 上限值
     * @return
     */
    @ApiOperation(value = "修改控制设备的上限值")
    @PostMapping(value = "/updateOnLimit")
    public Response updateOnLimit(Long id, double value) {
        monitorService.updateOnLimit(id, value);
        return Response.ok("修改上限值成功");
    }


    /**
     * 修改控制设备的下限值
     * @param id 控制设备id
     * @param value 下限值
     * @return
     */
    @ApiOperation(value = "修改控制设备的下限值")
    @PostMapping("/updateUpLimit")
    public Response updateUpLimit(Long id, double value) {
        monitorService.updateUpLimit(id, value);
        return Response.ok("修改下限值成功");
    }


    /**
     * 查询站房分管下机组分页的设备属性数据
     * @param pageable 分页参数，包括 page：当前页码，从0开始  size：每页显示记录数
     * @param id 机组菜单id
     * @return
     */
    @ApiOperation(value = "查询站房分管下机组分页的设备属性数据")
    @PostMapping("/partManageDevice")
    public Response partManageDevice(Pageable pageable, Long id) {
        PageVo result = monitorService.partManageDevice(pageable, id);
        return Response.ok(result);
    }


    /**
     * 查询设备的运行状态，运维数据：包括报警次数，运维次数，点检次数
     * @param params 参数：设备id和设备名称
     * @return
     */
    @ApiOperation(value = "查询设备的运行状态，运维数据：包括报警次数，运维次数，点检次数")
    @PostMapping(value = "/maintainData", produces = "application/json")
    public Response maintainData(@RequestBody Map params) {
        Long devId = ((Integer) params.get("devId")).longValue();
        String devName = (String) params.get("devName");
        Map maintainData = monitorService.maintainData(devId);
        Map devData = monitorService.modeRunStatus(devId);
        int runStatus = (int) devData.get("runStatus");
        Map imagePath = ImageUtil.getImagePath(devName, runStatus);
        Map map = new HashMap<String, Object>();
        map.put("controlMode", devData.get("controlMode"));
        map.put("runStatus", devData.get("runStatus"));
        map.put("alarmNum", maintainData.get("alarmNum"));
        map.put("maintainNum", maintainData.get("maintainNum"));
        map.put("checkNum", maintainData.get("checkNum"));
        map.put("devImagePath", imagePath.get("devImagePath"));
        map.put("backgroundPath", imagePath.get("backgroundPath"));
        return Response.ok(map);
    }


    /**
     * 查询设备所有属性的数据
     * @param devId 设备id
     * @return
     */
    @ApiOperation(value = "查询设备所有属性的数据")
    @PostMapping("/devItemsData")
    public Response devItemsData(Long devId) {
        Map[] result = monitorService.devItemsData(devId);
        return Response.ok(result);
    }


    /**
     * 查询设备某个属性当天的曲线数据
     * @param  devId 设备id
     * @param  name 属性名称
     * @return
     */
    @ApiOperation(value = "查询设备某个属性当天的曲线数据")
    @PostMapping("/devItemData")
    public Response devItemData(Long devId, String name) throws ParseException {
        Map result = monitorService.devItemData(devId, name);
        return Response.ok(result);
    }


    /**
     * 查询站房指定月份的供出，用量，损耗的统计数据
     * @param id 站房空间id
     * @param time 时间
     * @return
     */
    @ApiOperation(value = "查询供出，用量，损耗的统计数据")
    @PostMapping("/stationDosage")
    public Response stationDosage(Long id, String time) {
        Map resultMap = monitorService.stationDosage(id, time);
        return Response.ok(resultMap);
    }


    /**
     * 查询站房指定数据类型最近半年的数据
     * @param id 站房空间id
     * @param dataType 数据类型：1：供出 2：用量 3：损耗
     * @param time 时间
     * @return
     */
    @ApiOperation(value = "查询站房指定类型的数据")
    @PostMapping("/dosageTrend")
    public Response dosageTrend(Long id, int dataType, String time) throws ParseException {
        Map result = monitorService.dosageTrend(id, dataType, time);
        return Response.ok(result);
    }


    /**
     * 查询用能排行榜数据
     * @param id 站房空间id
     * @param time 时间
     * @return
     */
    @ApiOperation(value = "查询用能排行榜数据")
    @PostMapping("/dosageRanking")
    public Response dosageRanking(Long id, String time) {
        Map[] result = monitorService.dosageRanking(id, time);
        return Response.ok(result);
    }

}
